﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Presentation.Pages
{
    public partial class CadastrarVersiculo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!Util.usuarioEhAdmin(this.Context))
                    Response.Redirect("~/Pages/index.aspx");
                else
                    carregarVersiculos();
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                if (Util.usuarioEhAdmin(this.Context))
                {
                    DEVersiculo versiculo = new DEVersiculo();
                    versiculo.Versiculo = txtVersiculo.Text;

                    using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                    {
                        if (!banco.DEVersiculoes.Any(q => q.Versiculo == versiculo.Versiculo))
                        {
                            banco.AddToDEVersiculoes(versiculo);
                            banco.SaveChanges();

                            this.txtVersiculo.Text = String.Empty;
                            carregarVersiculos();
                            Util.showMessage(this, "Versículo cadastrado com sucesso");
                        }
                        else
                            Util.showMessage(this, "Versículo já cadastrado");
                    }
                }
                else
                    Response.Redirect("~/Pages/index.aspx");
            }
            catch (Exception)
            {
                Util.showMessage(this, "Houve um erro ao cadastrar um novo versículo");
            }
        }

        private void carregarVersiculos()
        {
            try
            {
                using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                {
                    List<DEVersiculo> versiculos = banco.DEVersiculoes.OrderBy(q => q.Codigo).ToList();

                    grvVersiculo.DataSource = versiculos;
                    grvVersiculo.DataBind();
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Erro ao carregar versículos");
            }
        }

        protected void btnExcluir_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Util.usuarioEhAdmin(this.Context))
                    Response.Redirect("~/Pages/index.aspx");
                else
                {
                    Button btnExcluir = sender as Button;
                    Int32 id = Convert.ToInt32(btnExcluir.CommandArgument);

                    using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                    {
                        DEVersiculo versiculo = banco.DEVersiculoes.Single(q => q.Codigo.Equals(id));

                        banco.DEVersiculoes.DeleteObject(versiculo);
                        banco.SaveChanges();
                    }

                    carregarVersiculos();

                    Util.showMessage(this, "Excluído com sucesso");
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Erro ao excluir versículo");
            }
        }
    }
}